package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.SearchParams;
import com.yuandian.api.management.entity.ConfSystem;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.enums.ResultCodeEnum;
import com.yuandian.management.service.ConfSystemService;
import com.yuandian.management.vo.SearchOptionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 系统配置表-Controller
 * @author zfsn
 * @date 2023-3-7
 */
@Api(tags = "业务系统配置-应用系统配置")
@RestController
@RequestMapping("/system")
public class ConfSystemController {

	@Autowired
	private ConfSystemService service;

	/**
	 * 通过主键查询数据
	 * @param systemId 主键集合
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "系统id", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{systemId}")
	public R<ConfSystem> queryById(@PathVariable("systemId") Integer systemId) {
		return R.ok(service.getById(systemId));
	}

	/**
	 * 分页查询
	 * @param confSystem 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "系统id", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "系统名称(支持模糊查询)", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "domainCode", value = "所属域", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "status", value = "被监控系统状态，0：初始化(默认)、1：监控中、2：停止", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "type", value = "系统类型，1：营业厅、2：网厅", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isRegion", value = "是否存在区域，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isKey", value = "是否为关键系统，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isVirtual", value = "是否虚拟系统，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cloudPlatformCode", value = "系统所属云平台编码", paramType = "body", required = false, dataType = "String"),
	})
	public R<Page<ConfSystem>> listPage(@RequestBody @ApiIgnore ConfSystem confSystem) {
		return R.ok(service.listPage(confSystem));
	}

	/**
	 * 新增数据
	 * @param confSystem 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增系统配置表数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "系统名称(支持模糊查询)", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "domainCode", value = "所属域", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isRegion", value = "是否存在区域，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "status", value = "被监控系统状态，0：初始化(默认)、1：监控中、2：停止", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isKey", value = "是否为关键系统，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "type", value = "系统类型，1：营业厅、2：网厅", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isVirtual", value = "是否虚拟系统，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "sequence", value = "显示顺序", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cloudPlatformCode", value = "系统所属云平台编码", paramType = "body", required = false, dataType = "String"),
	})
	public R<String> save(@Validated({InsertFlag.class, Default.class}) @RequestBody @ApiIgnore ConfSystem confSystem) {
		if(StringUtils.isEmpty(confSystem.getName())) { return R.failed("系统名称不能为空"); }
		return service.saveSystem(confSystem);
	}

	/**
	 * 通过主键更新数据
	 * @param confSystem 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新系统配置表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "系统id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "系统名称(支持模糊查询)", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "domainCode", value = "所属域", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "isRegion", value = "是否存在区域，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "status", value = "被监控系统状态，0：初始化(默认)、1：监控中、2：停止", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isKey", value = "是否为关键系统，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isDrift", value = "是否存在漂移，0:否(默认)、1:是", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "type", value = "系统类型，1：营业厅、2：网厅", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isVirtual", value = "是否虚拟系统，0:否(默认)、1:是", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "sequence", value = "显示顺序", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cloudPlatformCode", value = "系统所属云平台编码", paramType = "body", required = false, dataType = "String"),
	})
	public R<String> updateById(@Validated({UpdateFlag.class, Default.class}) @RequestBody @ApiIgnore ConfSystem confSystem) {
		if(StringUtils.isEmpty(confSystem.getName())) { return R.failed("系统名称不能为空"); }

		if(service.getById(confSystem.getId()) == null){
			return R.failed("该数据不存在");
		}
		if(service.updateById(confSystem)){
			return R.ok();
		}
		return R.failed(ResultCodeEnum.SYSTEM_NAME_EXIST.getMsg());
	}

	/**
	 * 通过主键删除数据
	 * @param systemIds 主键
	 * @return 通用返回对象
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemIds", value = "系统id(可多选)", paramType = "query", required = true, dataType = "Integer"),
	})
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@DeleteMapping("/delete/{systemIds}")
	public R<String> removeById(@PathVariable("systemIds") @ApiIgnore List<Integer> systemIds){
		if (!service.removeByIds(systemIds)) {
			return R.failed("请先删除该系统下的业务和端！或者该系统不存在");
		}
		return R.ok("删除系统成功");
	}


	/**
	 * 批量启动系统
	 *
	 * @param ids 系统id
	 * @return 返回对象
	 */
	@GetMapping("/startSystem/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "系统id(可多选)", paramType = "query", required = true, dataType = "Integer")
	})
	@ApiOperation(value = "批量启动系统", notes = "批量启动系统")
	public R<String> startSystemStatus(@PathVariable("ids") @ApiIgnore List<Integer> ids) {
		return service.changeSystemStatus(ids, true);
	}

	/**
	 * 批量停止系统
	 *
	 * @param ids 系统id
	 * @return 返回对象
	 */
	@GetMapping("/stopSystem/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "系统id(可多选)", paramType = "query", required = true, dataType = "Integer")
	})
	@ApiOperation(value = "批量停止系统", notes = "批量停止系统")
	public R<String> stopSystemStatus(@PathVariable("ids") List<Integer> ids) {
		return service.changeSystemStatus(ids, false);
	}

	/**
	 * 通过域code 获取该域下所有的系统(远程调用接口)
	 * @param domainCode 域code
	 * @return 返回对象
	 */
	@ApiIgnore
	@GetMapping("/getSystemsByDomainCode/{domainCode}")
	public R<List<ConfSystem>> getSystemsByDomainId(@PathVariable("domainCode") String domainCode) {
		QueryWrapper<ConfSystem> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("domain_code", domainCode);
		return R.ok(service.list(queryWrapper));
	}

	/**
	 * 通过域code 获取该域下所有的系统(远程调用接口)
	 * @return 返回对象
	 */
	@ApiIgnore
	@GetMapping("/getSystemsAndDomain")
	public R<List<ConfSystem>> getSystemsAndDomain() {
		return R.ok(service.getSystemsAndDomain());
	}

	/**
	 * 通用查询(远程调用接口)
	 * @param confSystem 筛选条件
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@PostMapping("/list")
	public R<List<ConfSystem>> list(@RequestBody ConfSystem confSystem) {
		return R.ok(service.list(Wrappers.query(confSystem)));
	}

	/**
	 * 查询默认的应用系统选项列表
	 * @param params 搜索项请求参数
	 * @return 搜索选项vo对象集合
	 */
	@PostMapping("/queryDefaultAppSystemOptions")
	public R<List<SearchOptionVo>> queryDefaultAppSystemOptions(@RequestBody SearchParams params){
		List<SearchOptionVo> searchOptionVos = service.queryDefaultAppSystemOptions(params);
		return R.ok(searchOptionVos);
	}
}
